# coding=utf8
# 全世界最帅的帅哥
from flask_restful import Resource, reqparse
from comment.modules.matched_result import MatchedResult
from comment.modules import db
from .serializer import MatchedResultPaginateSerializer
from comment.utils.decorators import root_login_required


class MatchedResultResource(Resource):
    """匹配结果列表的搜索查询功能"""
    method_decorators = [root_login_required]

    def get(self):
        rp = reqparse.RequestParser()
        rp.add_argument("start")  # 开始时间
        rp.add_argument("end")  # 结束时间
        rp.add_argument("curPage")  # 当前页
        rp.add_argument("perPage")  # 每一页显示的数据

        args = rp.parse_args()
        start = args.start
        end = args.end
        cur_page = int(args.curPage)
        per_page = int(args.perPage)

        # 过滤查询
        # 1.审批状态
        matched_result_query = MatchedResult.query
        # 2.时间
        if start:
            matched_result_query = matched_result_query.filter(
                db.cast(MatchedResult.matchDate, db.DATE) <= db.cast(start, db.DATE))
        if end:
            matched_result_query = matched_result_query.filter(
                db.cast(MatchedResult.matchDate, db.DATE) <= db.cast(end, db.DATE))
        matched_result_list = matched_result_query.paginate(page=cur_page, per_page=per_page, error_out=False)
        matched_result_data = MatchedResultPaginateSerializer(matched_result_list).to_dict()
        return {"msg": "查询匹配结果数据成功", "data": matched_result_data}
